package finedev.work.vennue;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.android.FacebookError;
import com.facebook.android.Util;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class FindFriendActivity extends Activity {
	
	private Activity mActivity;
	
	private final String TAG = "FindFriend";
	
	private ProgressDialog progressDialog;
	
	private ListView mListView;
	private FriendListAdapter mFriendListAdapter;
	private ArrayList<FriendItem> mFriendListData = new ArrayList<FriendItem>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_friend);
        
        mActivity = this;
        
        mListView = (ListView) findViewById(R.id.findfriend_listview);
        mFriendListAdapter = new FriendListAdapter( this, R.layout.findfriend_row, mFriendListData );
        mListView.setAdapter( mFriendListAdapter );
        
        getFriendList();
    }
    
    private void getFriendList() {
    	
    	Log.i( "FindFriend","getFriendList()" );
    	
    	String query = "SELECT uid, name, pic_small, religion FROM user WHERE is_app_user = '1' and uid in ( SELECT uid2 FROM friend WHERE uid1 = me() )";
    	
    	Bundle params = new Bundle();
        params.putString("method", "fql.query");
        params.putString("query", query);
        
        progressDialog = new ProgressDialog( mActivity );
		progressDialog.setMessage("get friend list...");
		progressDialog.setCancelable(false);
		
		progressDialog.show();
		
        Utility.mAsyncFacebookRunner.request(null, params,
                                                    new FriendsRequestListener());
        
        
    	
    }
    
    final Handler ListViewUpdateHandler = new Handler()
    {
    	public void handleMessage(android.os.Message msg) {
    		mFriendListAdapter.notifyDataSetChanged();
    	}
    };
    
    public class FriendsRequestListener extends BaseRequestListener {

        @Override
        public void onComplete(final String response, final Object state) 
        {
        	Log.i(TAG, response );
        	
        	JSONObject resultObj = null;
        	JSONArray jArray = null;
			try 
			{
				jArray = new JSONArray( response );
//				jArray = resultObj.getJSONArray("data");
				Log.i(TAG, "jArray");
				if ( jArray == null ) return;
				for ( int i=0; i<jArray.length(); i++ ) {
					JSONObject obj = jArray.getJSONObject(i);
					FriendItem friendItem = new FriendItem( obj.getString("name"), obj.getString("pic_small"), obj.getString("religion"));
					Log.i(TAG, friendItem.getName() );
					mFriendListData.add( friendItem );
//					Log.i(TAG, "username:"+obj.getString("username"));
//					Log.i(TAG, "pic_small:"+obj.getString("pic_small"));
//					Log.i(TAG, "religion:"+obj.getString("religion"));
				}
				ListViewUpdateHandler.sendEmptyMessage( 0 );
			} catch (FacebookError e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			progressDialog.dismiss();
        	
        	
//            dialog.dismiss();
//            Intent myIntent = new Intent(getApplicationContext(), FriendsList.class);
//            myIntent.putExtra("API_RESPONSE", response);
//            myIntent.putExtra("METHOD", graph_or_fql);
//            startActivity(myIntent);
        }

        public void onFacebookError(FacebookError error) {
        	progressDialog.dismiss();
        	Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(),
                  Toast.LENGTH_SHORT).show();
//            dialog.dismiss();
//            Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(),
//                    Toast.LENGTH_SHORT).show();
        }
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.activity_find_friend, menu);
//        return true;
//    }
}